﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class GoodsClassAuthority
    {
        public int Add(Chain.Model.GoodsClassAuthority model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into GoodsClassAuthority(");
            builder.Append("ClassID,ShopID)");
            builder.Append(" values (");
            builder.Append("@ClassID,@ShopID)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ClassID", SqlDbType.Int, 4), new SqlParameter("@ShopID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.ClassID;
            cmdParms[1].Value = model.ShopID;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int ClassAuthorityID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from GoodsClassAuthority ");
            builder.Append(" where ClassAuthorityID=@ClassAuthorityID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ClassAuthorityID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ClassAuthorityID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public int DeleteAuthority(int ClassID, int ShopID)
        {
            string format = "delete from GoodsClassAuthority where ClassID = {0} and ShopID = {1}";
            return DbHelperSQL.ExecuteSql(string.Format(format, ClassID, ShopID));
        }

        public bool DeleteList(string ClassAuthorityIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from GoodsClassAuthority ");
            builder.Append(" where ClassAuthorityID in (" + ClassAuthorityIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public int GetClassCountByParentID(int ParentID)
        {
            string format = "select count(0) from GoodsClass where ParentID = {0}";
            return int.Parse(DbHelperSQL.GetSingle(string.Format(format, ParentID)).ToString());
        }

        public int GetClassCountByParentID(int ParentID, int ShopID)
        {
            string format = "select count(0) from GoodsClass,GoodsClassAuthority \r\n                where GoodsClass.ClassID = GoodsClassAuthority.ClassID and ParentID = {0} and GoodsClassAuthority.ShopID = {1}";
            return int.Parse(DbHelperSQL.GetSingle(string.Format(format, ParentID, ShopID)).ToString());
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ClassAuthorityID,ClassID,ShopID ");
            builder.Append(" FROM GoodsClassAuthority ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" ClassAuthorityID,ClassID,ShopID ");
            builder.Append(" FROM GoodsClassAuthority ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.ClassAuthorityID desc");
            }
            builder.Append(")AS Row, T.*  from GoodsClassAuthority T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public Chain.Model.GoodsClassAuthority GetModel(int ClassAuthorityID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 ClassAuthorityID,ClassID,ShopID from GoodsClassAuthority ");
            builder.Append(" where ClassAuthorityID=@ClassAuthorityID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ClassAuthorityID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ClassAuthorityID;
            Chain.Model.GoodsClassAuthority authority = new Chain.Model.GoodsClassAuthority();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["ClassAuthorityID"] != null) && (set.Tables[0].Rows[0]["ClassAuthorityID"].ToString() != ""))
            {
                authority.ClassAuthorityID = int.Parse(set.Tables[0].Rows[0]["ClassAuthorityID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ClassID"] != null) && (set.Tables[0].Rows[0]["ClassID"].ToString() != ""))
            {
                authority.ClassID = int.Parse(set.Tables[0].Rows[0]["ClassID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ShopID"] != null) && (set.Tables[0].Rows[0]["ShopID"].ToString() != ""))
            {
                authority.ShopID = int.Parse(set.Tables[0].Rows[0]["ShopID"].ToString());
            }
            return authority;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM GoodsClassAuthority ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public DataSet GetShopIDListByClass(int ClassID)
        {
            return DbHelperSQL.Query("select shopid from GoodsClassAuthority where ClassID = " + ClassID);
        }

        public int SyncGoodsClass(int ClassID)
        {
            string format = "insert into GoodsClassAuthority(ClassID,ShopID) \r\n                (select {0},ShopID from SysShop where ShopID>0 and ShopID not in(SELECT ShopID FROM GoodsClassAuthority where ClassID = {0}))";
            int num = DbHelperSQL.ExecuteSql(string.Format(format, ClassID));
            if (num > 0)
            {
                string str2 = "INSERT INTO GoodsClassDiscount (GoodsClassID,MemLevelID,DiscountShopID,ClassDiscountPercent,ClassPointPercent) \r\n                    (select {0},LevelID,ShopID,1,1 from SysShop,MemLevel where ShopID>0 and ShopID not in (SELECT DiscountShopID FROM GoodsClassDiscount where GoodsClassID = {0}))";
                return DbHelperSQL.ExecuteSql(string.Format(str2, ClassID));
            }
            return num;
        }

        public int SyncGoodsClass(int ClassID, List<int> ShopList)
        {
            if (ShopList.Count <= 0)
            {
                return 0;
            }
            string str = string.Empty;
            foreach (int num in ShopList)
            {
                str = str + num + ",";
            }
            str = str.Trim(new char[] { ',' });
            string format = "insert into GoodsClassAuthority(ClassID,ShopID) \r\n                (select {0},ShopID from SysShop where ShopID>0 and ShopID in ({1}) and ShopID not in(SELECT ShopID FROM GoodsClassAuthority where ClassID = {0}))";
            int num2 = DbHelperSQL.ExecuteSql(string.Format(format, ClassID, str));
            if (num2 > 0)
            {
                string str3 = "INSERT INTO GoodsClassDiscount(GoodsClassID,MemLevelID,DiscountShopID,ClassDiscountPercent,ClassPointPercent) \r\n                    (select {0},LevelID,ShopID,1,0.01 from SysShop,MemLevel where ShopID>0 and ShopID in ({1}) and ShopID not in (SELECT DiscountShopID FROM GoodsClassDiscount where GoodsClassID = {0}))";
                return DbHelperSQL.ExecuteSql(string.Format(str3, ClassID, str));
            }
            return num2;
        }

        public int SyncGoodsClass(int ClassID, int ShopID)
        {
            string format = "insert into GoodsClassAuthority(ClassID,ShopID) \r\n                (select {0},ShopID from SysShop where ShopID>0 and ShopID = {1} and ShopID not in(SELECT ShopID FROM GoodsClassAuthority where ClassID = {0}))";
            int num = DbHelperSQL.ExecuteSql(string.Format(format, ClassID, ShopID));
            if (num > 0)
            {
                string str2 = "INSERT INTO GoodsClassDiscount(GoodsClassID,MemLevelID,DiscountShopID,ClassDiscountPercent,ClassPointPercent) \r\n                    (select {0},LevelID,ShopID,1,0.01 from SysShop,MemLevel where ShopID>0 and ShopID = {1} and ShopID not in (SELECT DiscountShopID FROM GoodsClassDiscount where GoodsClassID = {0}))";
                return DbHelperSQL.ExecuteSql(string.Format(str2, ClassID, ShopID));
            }
            return num;
        }

        public bool Update(Chain.Model.GoodsClassAuthority model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update GoodsClassAuthority set ");
            builder.Append("ClassID=@ClassID,");
            builder.Append("ShopID=@ShopID");
            builder.Append(" where ClassAuthorityID=@ClassAuthorityID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ClassID", SqlDbType.Int, 4), new SqlParameter("@ShopID", SqlDbType.Int, 4), new SqlParameter("@ClassAuthorityID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.ClassID;
            cmdParms[1].Value = model.ShopID;
            cmdParms[2].Value = model.ClassAuthorityID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

